package com.hghivln.erpwarehouse.system.controller;


import com.hghivln.erpwarehouse.system.common.lang.DataGridlView;
import com.hghivln.erpwarehouse.system.common.lang.Result;
import com.hghivln.erpwarehouse.system.entity.Dept;
import com.hghivln.erpwarehouse.system.service.DeptService;
import com.hghivln.erpwarehouse.system.vo.DeptVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hghivln
 * @since 2021-06-27
 */
@RestController
@RequestMapping("api/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("loadAllDept")
    public Object loadAllDept(DeptVo deptVo){
        return deptService.queryAllDept(deptVo);
    }

    @RequestMapping("addDept")
    @RequiresPermissions("dept:add")
    public Result addDept(DeptVo deptVo){
        try {
            deptVo.setCreatetime(new Date());
            deptService.saveDept(deptVo);
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    /**
     * 查询最大排序码
     */
    @RequestMapping("queryDeptMaxOrderNum")
    public Object queryDeptMaxOrderNum(){
        return deptService.queryDeptMaxOrderNum();
    }

    @RequestMapping("updateDept")
    @RequiresPermissions("dept:update")
    public Result updateDept(DeptVo deptVo){
        try {
            deptService.updateDept(deptVo);
            return Result.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    @RequestMapping("getDeptById")
    public DataGridlView getDeptById(Integer id) {
        Dept dept = deptService.getById(id);
        return new DataGridlView(dept);
    }

    /**
     * 根据ID查询当前部门的子部门的个数
     */
    @RequestMapping("getDeptChildrenCountById")
    public Object getDeptChildrenCountById(Integer id) {
        return deptService.getDeptChildrenCountById(id);
    }

    @RequestMapping("deleteDept")
    @RequiresPermissions("dept:delete")
    public Result deleteDept(Integer id) {
        try {
            deptService.removeById(id);
            return Result.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }
}
